import util from '@/libs/util';
import CONSTANTS from '@/config/constants';
import getDate from '@/util/getDate.js';
import Cookies from 'js-cookie';

export default {
    name: 'exportable-table',
    data () {
        return {
            columns: [{
                title: '序号',
                key: 'rownum',
                align: 'center',
                width: 60,
                render: (h, params) => {
                    return h('div', {}, ((this.pageIndex - 1) * this.pageSize + params.index + 1));
                }
            }, {
                title: '用户名称',
                key: 'realname'
            }, {
                title: '联系电话',
                key: 'phone'
            }, {
                title: '性别',
                key: 'sex',
                render: (h, params) => {
                    return h('div', {}, (params.row.sex === 1 ? '男' : '女'));
                }
            }, {
                title: '年龄',
                key: 'age'
            }, {
                title: '学校/所在单位',
                key: 'school'
            }, {
                title: '操作',
                key: 'action',
                width: 200,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.showEdit(params.row);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.memberid = params.row.id;
                                    this.prizesSelect();
                                }
                            }
                        }, '我的奖状'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            on: {
                                click: () => {
                                    this.delete(params.row.id);
                                }
                            }
                        }, '删除')
                    ]);
                }
            }],
            baseuser: JSON.parse(Cookies.get('baseUser')),
            data: [],
            formData: {},
            query: {
                realname: '',
                phone: '',
                idcard: ''
            },

            total: 0,
            pageIndex: 1,
            pageSize: 10,

            prizesColumns: [
                {title: '主题', key: 'themeid', render: (h, params) => { return h('div', {}, params.row.theme.name); }},
                {title: '奖项', key: 'diplomasid', render: (h, params) => { return h('div', {}, params.row.diplomas.name); }},
                {title: '所属机构', key: 'orgid', render: (h, params) => { return h('div', {}, params.row.organize.orgName); }},
                {
                    title: '奖状生成时间',
                    key: 'createtime',
                    render: (h, params) => { return h('div', {}, getDate.getDate(params.row.createtime, 'time')); }
                },
                {title: '操作',
                    key: 'action',
                    width: 250,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.prizeModal = true;
                                        this.imgpath = CONSTANTS.SERVER_HOST + params.row.imgpath;
                                    }
                                }
                            }, '查看奖状'),
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.$Modal.confirm({
                                            title: '提示',
                                            content: '<p>确定删除？</p>',
                                            onOk: () => {
                                                util.ajax.post('/busine/prizes/delete', util.toFormData({
                                                    id: params.row.id
                                                })).then(res => {
                                                    if (res.data.message === 'SUCCESS') {
                                                        this.$Message.success('删除成功！');
                                                        this.prizesSelect();
                                                    }
                                                });
                                            },
                                            onCancel: () => {
                                            }
                                        });
                                    }
                                }
                            }, '删除'),
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.imgpath = CONSTANTS.SERVER_HOST + params.row.imgpath;
                                        util.toPDF(this.imgpath, params.row.theme.name + '-' + params.row.diplomas.name);
                                    }
                                }
                            }, '下载PDF')
                        ]);
                    }
                }
            ],
            prizesModal: false,
            prizesData: [],
            prizesTotal: 0,
            prizesIndex: 1,
            prizesSize: 10,
            prizeModal: false,
            imgpath: '',
            memberid: '',
            editModal: false,
            SERVER_HOST: CONSTANTS.SERVER_HOST,
            downloadUrl: CONSTANTS.LOCAL_HOST + '/static/file/memberImport.xlsx'
        };
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList () {
            util.ajax.post('/busine/member/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                realname: this.query.realname,
                phone: this.query.phone,
                idcard: this.query.idcard,
                orgid: this.baseuser.orgId
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        refresh () {
            this.query = {
                realname: '',
                phone: '',
                idcard: ''
            };
            this.loadList();
        },
        prizesSelect () {
            util.ajax.post('/busine/prizes/list', util.toFormData({
                page: this.prizesIndex,
                size: this.prizesSize,
                memberid: this.memberid
            })).then(res => {
                this.prizesData = res.data.data.list;
                this.prizesTotal = res.data.data.total;
                this.prizesModal = true;
            });
        },
        prizeDownload (id) {
            var link = document.createElement('a');
            link.download = 'my-image-name.jpeg';
            link.href = this.imgpath;
            link.click();
            util.ajax.post('/busine/prizes/prizeDownload', util.toFormData({
                id: id
            })).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.$Message.success('下载完成！');
                    this.prizesSelect();
                }
            });
        },
        showAdd () {
            this.formData = {};
            this.editModal = true;
        },
        save () {
            let formData = {};
            for (let key in this.formData) {
                formData[key] = this.formData[key];
            }
            formData['orgid'] = this.baseuser.orgId;
            if (!formData.realname) {
                this.$Modal.error({
                    content: '会员名称不能为空！'
                });
                return;
            }
            if (!formData.phone) {
                this.$Modal.error({
                    content: '联系电话不能为空！'
                });
                return;
            }
            util.ajax.post('/busine/member/save', util.toFormData(formData)).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.$Message.success('保存成功！');
                    this.loadList();
                    this.editModal = false;
                } else {
                    this.$Modal.error({
                        content: res.data.message || '未知错误！'
                    });
                }
            });
        },
        showEdit (obj) {
            for (let key in obj) {
                this.formData[key] = obj[key];
            }
            this.editModal = true;
        },
        delete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '<p>确定删除？</p>',
                onOk: () => {
                    util.ajax.post('/busine/member/delete', util.toFormData({
                        id: id
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('删除成功！');
                            this.loadList();
                        }
                    });
                },
                onCancel: () => {
                }
            });
        },
        handleSuccess (res, file) {
            if (res.message === 'SUCCESS') {
                this.$Notice.success({
                    title: '文件上传成功',
                    desc: '会员导入成功',
                    duration: 2
                });
                this.loadList();
            } else {
                this.$Modal.error({
                    content: res.message
                });
            }
        }
    }
}